package cn.wolfcode.shop.cloud.redis;

import cn.wolfcode.shop.cloud.common.BusinessException;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

public class RedisService {
    @Autowired
    private JedisPool jedisPool;

    //set
    public <T> void set(KeyPrefix keyPrefix, String key, T data) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            //判断是否有过期时间，如果有设置，没有不需要设置
            if (keyPrefix.getExpireTime() > 0) {
                jedis.setex(realKey, keyPrefix.getExpireTime(), JSON.toJSONString(data));
            } else {
                jedis.set(realKey, JSON.toJSONString(data));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //set
    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> clz) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            //从redis中拿值
            String redisVal = jedis.get(realKey);
            //如果为空返回null
            if (redisVal == null) {
                return null;
            }
            return JSON.parseObject(redisVal, clz);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //incr
    public Long incr(KeyPrefix keyPrefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.incr(realKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //decr
    public Long decr(KeyPrefix keyPrefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.decr(realKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //expire
    public Long expire(KeyPrefix keyPrefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.expire(realKey, keyPrefix.getExpireTime());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //exists
    public Boolean exits(KeyPrefix keyPrefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            return jedis.exists(realKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hset
    public <T> void hset(KeyPrefix keyPrefix, String key, String field, T data) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            jedis.hset(realKey, field, JSON.toJSONString(data));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hget
    public <T> T hget(KeyPrefix keyPrefix, String key, String field, Class<T> clz) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            return JSON.parseObject(jedis.hget(realKey, field), clz);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hgetAll
    public <T> Map<String, T> hgetAll(KeyPrefix keyPrefix, String key, Class<T> clz) {
        try (Jedis jedis = jedisPool.getResource()) {
            //拼接真正的key存到redis的key
            String realKey = keyPrefix.getKeyPrefix() + key;
            Map<String, String> stringMap = jedis.hgetAll(realKey);
            Map<String, T> map = new HashMap<>();
            for (Map.Entry<String, String> stringEntry : stringMap.entrySet()) {
                map.put(stringEntry.getKey(), JSON.parseObject(stringEntry.getValue(), clz));
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

}
